﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardsServer.DAL;
using System.ServiceModel;
using CardsServer.WCF;
using System.Linq.Expressions;
using LinqToSqlExtensions;
using System.Transactions;

namespace CardsServer.WCF
{
    /// <summary>
    /// 产品销售方法
    /// </summary>
    public partial interface IService
    {
        #region 销售出库单的接口方法 
        
        /// <summary>
        /// 查询仓库中的产品列表
        /// </summary>
        /// <param name="ckid"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<View_Goods_Lite> Product_Get(string ckid, int ACID);

        /// <summary>
        /// 产品销售保存
        /// </summary>
        /// <param name="m"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        string ProductSaleSave(ProductSale_M m, int ACID);
        
        /// <summary>
        /// 查询所有需要审核的销售单
        /// </summary>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<View_ProductSale_M> NoCheckProductSale_Get(int ACID);

        /// <summary>
        /// 审核产品销售单
        /// </summary>
        /// <param name="m"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        string CheckProductSale(ProductSale_M m, int ACID);

        /// <summary>
        /// 通过条件查询销售单
        /// </summary>
        /// <param name="field"></param>
        /// <param name="logic"></param>
        /// <param name="value"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<View_ProductSale_M> GetProductSale_M_ByCondition(string field, string logic, string value, int ACID);

        /// <summary>
        /// 产品销售--修改方法
        /// </summary>
        /// <param name="m">产品销售主表</param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        string ProductSale_UpdateSave(ProductSale_M m, int ACID);

        /// <summary>
        /// 删除销售出库单
        /// </summary>
        /// <param name="djn"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        string ProductSale_Delete(string djn, int ACID);


        #endregion

        #region 销售退货单定义的接口方法

        /// <summary>
        /// 产品销售退货保存
        /// </summary>
        /// <param name="m"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        string ProductSaleBack_AddSave(ProductSaleBack_M m, int ACID);

        /// <summary>
        /// 销售退货--修改方法
        /// </summary>
        /// <param name="m">产品销售主表</param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        string ProductSaleBack_UpdateSave(ProductSaleBack_M m, int ACID);

        /// <summary>
        /// 删除销售退货单
        /// </summary>
        /// <param name="djn"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        string ProductSaleBack_Delete(string djn, int ACID);


        /// <summary>
        /// 通过条件查询销售退货单
        /// </summary>
        /// <param name="field">字段</param>
        /// <param name="logic">逻辑比较</param>
        /// <param name="value">比较值</param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<View_ProductSaleBack_M> GetProductSaleBack_M_ByCondition(string field, string logic, string value, int ACID);

        #endregion

        #region 销售分析报表

        /// <summary>
        /// 产品交易销售汇总表
        /// </summary>
        /// <param name="ckid"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<Get_ProductSale_ByGoodsResult> Get_ProductSale_ByGoodsResult(DateTime? sd, DateTime? ed, int ACID);

        /// <summary>
        /// 客户销售交易汇总表
        /// </summary>
        /// <param name="ckid"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<Get_ProductSale_ByClientResult> Get_ProductSale_ByClientResult(DateTime? sd, DateTime? ed, int ACID);

        /// <summary>
        /// 业务员销售交易汇总表
        /// </summary>
        /// <param name="ckid"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<Get_ProductSale_ByEmpResult> Get_ProductSale_ByEmpResult(DateTime? sd, DateTime? ed, int ACID);

        #endregion

        #region 及时库存查询

        /// <summary>
        /// 及时库存
        /// </summary>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<View_Onhand> Get_Onhand(int ACID);


        #endregion
    }

    //实现类
    public partial class Service : IService
    {
        #region 销售出库单实现接口方法

        /// <summary>
        /// 通过仓库，查询产品
        /// </summary>
        /// <param name="ckid"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<View_Goods_Lite> Product_Get(string ckid, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext cxt=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                Expression<Func<View_Goods_Lite, bool>> predicate = PredicateBuilder.True<View_Goods_Lite>();
                predicate = p => p.Kind == 1; //库存商品是1
                if (!string.IsNullOrEmpty(ckid))
                {
                    predicate = predicate.And(p => p.WarehouseID == ckid);
                }

                return cxt.View_Goods_Lite.Where(predicate).ToList();
            }
        }

        /// <summary>
        /// 产品销售--新增方法
        /// </summary>
        /// <param name="m">产品销售主表</param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string ProductSaleSave(ProductSale_M m, int ACID)
        {
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        ProductSale_M_Add(m, ACID, ctx);
                        foreach (ProductSale_D d in m.ProductSale_D)
                        {
                            ProductSale_D_Add(d, ACID, ctx);
                        }
                        //减少库存
                        //但必须有条件，判断当前单据是否需要审核，如果明细中有销售金额小于了最低售价，需要审核
                        if (!m.ischeck)
                        {
                            foreach (var kc in m.ProductSale_D.GroupBy(p => p.goodscode))
                            {
                                onhand o = ctx.onhand.Single(p => p.GoodsCode == kc.Key && p.WarehouseID == m.warehouseID);

                                o.onhandcount -= kc.Sum(p => p.count);
                            }
                        }
                        RecordBalance(decimal.Parse(m.banlance.ToString()), m.clientID.Value, ctx, ACID);
                        ctx.SubmitChanges();
                        tc.Complete();
                        r = "";
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;



        }

        /// <summary>
        /// 产品销售--修改方法
        /// </summary>
        /// <param name="m">产品销售主表</param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string ProductSale_UpdateSave(ProductSale_M m, int ACID)
        {
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        //先获得修改前的对象
                        ProductSale_M oldPmitem = ctx.ProductSale_M.Single(p => p.djn == m.djn);
                        List<ProductSale_D> oldpdlist = ctx.ProductSale_D.Where(p => p.djn == m.djn).ToList();
                        ProductSale_M_Update(m, ACID, ctx);
                        //ProductSale_D_DeleteByDjn(m.djn, ACID, ctx); //先删除原来的明细
                        //再执行保存明细
                        foreach (ProductSale_D d in m.ProductSale_D)
                        {
                            ProductSale_D nd = new ProductSale_D();
                            nd.djn = d.djn;
                            nd.goodscode = d.goodscode;
                            nd.goodsname = d.goodsname;
                            nd.unit = d.unit;
                            nd.inprice = d.inprice;
                            nd.saleprice = d.saleprice;
                            nd.jsprice = d.jsprice;
                            nd.count = d.count;
                            nd.inamount = d.inamount;
                            nd.saleamount = d.saleamount;
                            nd.jsamount = d.jsamount;
                            nd.remark = d.remark;
                            ProductSale_D_Add(nd, ACID, ctx);
                        }
                        //删除原来的
                        foreach (ProductSale_D oldd in oldpdlist)
                        {
                            ProductSale_D_DeleteByID(oldd.zid, ACID, ctx);
                        }

                        //不需要审核的，说明已经减少了库存,需要审核的，说明之前库存没减少，不需要执行此操作
                        //那么删除明细时，就应该在库存表中加上原来减少的
                        if (!oldPmitem.ischeck)
                        {
                            foreach (var kc in oldpdlist.GroupBy(p => p.goodscode))
                            {
                                onhand o = ctx.onhand.Single(p => p.GoodsCode == kc.Key && p.WarehouseID == oldPmitem.warehouseID);

                                o.onhandcount += kc.Sum(p => p.count);
                            }
                        }

                        //减少库存
                        //但必须有条件，判断当前单据是否需要审核，如果明细中有销售金额小于了最低售价，需要审核
                        if (!m.ischeck)
                        {
                            foreach (var kc in m.ProductSale_D.GroupBy(p => p.goodscode))
                            {
                                onhand o = ctx.onhand.Single(p => p.GoodsCode == kc.Key && p.WarehouseID == m.warehouseID);

                                o.onhandcount -= kc.Sum(p => p.count);
                            }
                        }
                        RecordBalance(m.banlance.Value - oldPmitem.banlance.Value, m.clientID.Value, ctx, ACID);
                        ctx.SubmitChanges();
                        tc.Complete();
                        r = "";
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;



        }

        /// <summary>
        /// 删除销售出库单
        /// </summary>
        /// <param name="djn"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string ProductSale_Delete(string djn, int ACID)
        {
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        //先获得删除前的对象
                        ProductSale_M oldPmitem = ctx.ProductSale_M.Single(p => p.djn == djn);
                        List<ProductSale_D> oldpdlist = ctx.ProductSale_D.Where(p => p.djn == djn).ToList();

                        //删除原来的明细
                        foreach (ProductSale_D oldd in oldpdlist)
                        {
                            ProductSale_D_DeleteByID(oldd.zid, ACID, ctx);
                        }
                        ProductSale_MBydid_Delete(djn, ACID, ctx);


                        //不需要审核的，说明已经减少了库存,需要审核的，说明之前库存没减少，不需要执行此操作
                        //那么删除明细时，就应该在库存表中加上原来减少的
                        if (!oldPmitem.ischeck)
                        {
                            foreach (var kc in oldpdlist.GroupBy(p => p.goodscode))
                            {
                                onhand o = ctx.onhand.Single(p => p.GoodsCode == kc.Key && p.WarehouseID == oldPmitem.warehouseID);

                                o.onhandcount += kc.Sum(p => p.count);
                            }
                        }

                        RecordBalance(-oldPmitem.banlance.Value, oldPmitem.clientID.Value, ctx, ACID);
                        ctx.SubmitChanges();
                        tc.Complete();
                        r = "";
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }

        /// <summary>
        /// 得到需要审核的销售单
        /// </summary>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<View_ProductSale_M> NoCheckProductSale_Get(int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext cxt = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                Expression<Func<View_ProductSale_M, bool>> predicate = PredicateBuilder.True<View_ProductSale_M>();
                predicate = p => p.checkstatus == "待审核"; //未审核的
                return cxt.View_ProductSale_M.Where(predicate).ToList();
            }
        }

        /// <summary>
        /// 审核操作
        /// </summary>
        /// <param name="m"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string CheckProductSale(ProductSale_M m, int ACID)
        {
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        //先修改单据信息
                        ProductSale_M nm = ctx.ProductSale_M.Single(p => p.djn == m.djn);
                        if (nm.checkstatus != "待审核")
                        {
                            return "当前选择的销售单不需要审核！";
                        }
                        nm.checkstatus = m.checkstatus;
                        nm.ischeck = true; //审核后，就改为true
                        //修改状态
                        if (m.checkstatus == "已通过") //通过，减少库存,同时加付款单
                        {

                            foreach (var kc in nm.ProductSale_D.GroupBy(p => p.goodscode))
                            {
                                onhand o = ctx.onhand.Single(p => p.GoodsCode == kc.Key && p.WarehouseID == nm.warehouseID);

                                o.onhandcount -= kc.Sum(p => p.count);
                                
                            }
                            RecordBalance(decimal.Parse(nm.banlance.ToString()), nm.clientID.Value, ctx, ACID);
                        }
                        ctx.SubmitChanges();
                        tc.Complete();
                        r = "";
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }

        /// <summary>
        /// 通过条件查询销售单据
        /// </summary>
        /// <param name="field"></param>
        /// <param name="logic"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public List<View_ProductSale_M> GetProductSale_M_ByCondition(string field, string logic, string value, int ACID)
        {
            
            using (CardsServer.DAL.DataClassesDataContext cxt = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                Expression<Func<View_ProductSale_M, bool>> predicate = PredicateBuilder.True<View_ProductSale_M>();
                if (!string.IsNullOrEmpty(field) && !string.IsNullOrEmpty(logic) && !string.IsNullOrEmpty(value))
                {
                    #region 条件为等于

                    if (logic == "等于")
                    {
                        switch (field)
                        {
                            case "单据号":
                                predicate = predicate.And(p => p.djn.Equals(value));
                                break;
                            case "单据日期":
                                predicate = predicate.And(p => p.odate.Equals(value));
                                break;
                            case "客户名称":
                                predicate = predicate.And(p => p.ClientName.Equals(value));
                                break;
                            case "仓库名称":
                                predicate = predicate.And(p => p.WarehouseName.Equals(value));
                                break;
                            case "总金额":
                                predicate = predicate.And(p => p.banlance.Equals(value));
                                break;
                            case "已结金额":
                                predicate = predicate.And(p => p.ysmoney.Equals(value));
                                break;
                            case "联系人":
                                predicate = predicate.And(p => p.linkman.Equals(value));
                                break;
                            case "联系电话":
                                predicate = predicate.And(p => p.linktel.Equals(value));
                                break;
                            case "联系地址":
                                predicate = predicate.And(p => p.linkaddress.Equals(value));
                                break;
                            case "业务员":
                                predicate = predicate.And(p => p.jsusername.Equals(value));
                                break;
                        }
                    }
                    #endregion

                    #region 条件为包含

                    if (logic == "包含")
                    {
                        switch (field)
                        {
                            case "单据号":
                                predicate = predicate.And(p => p.djn.Contains(value));
                                break;
                            case "单据日期":
                                predicate = predicate.And(p => p.odate.ToString().Contains(value));
                                break;
                            case "客户名称":
                                predicate = predicate.And(p => p.ClientName.Contains(value));
                                break;
                            case "仓库名称":
                                predicate = predicate.And(p => p.WarehouseName.Contains(value));
                                break;
                            case "总金额":
                                predicate = predicate.And(p => p.banlance.ToString().Contains(value));
                                break;
                            case "已结金额":
                                predicate = predicate.And(p => p.ysmoney.ToString().Contains(value));
                                break;
                            case "联系人":
                                predicate = predicate.And(p => p.linkman.Contains(value));
                                break;
                            case "联系电话":
                                predicate = predicate.And(p => p.linktel.Contains(value));
                                break;
                            case "联系地址":
                                predicate = predicate.And(p => p.linkaddress.Contains(value));
                                break;
                            case "业务员":
                                predicate = predicate.And(p => p.jsusername.Contains(value));
                                break;
                        }
                    }
                    #endregion

                    #region 条件为大于

                    if (logic == "大于")
                    {
                        switch (field)
                        {

                            case "单据日期":
                                predicate = predicate.And(p => p.odate > DateTime.Parse(value));
                                break;
                            case "总金额":
                                predicate = predicate.And(p => p.banlance > decimal.Parse(value));
                                break;
                            case "已结金额":
                                predicate = predicate.And(p => p.ysmoney > decimal.Parse(value));
                                break;
                        }
                    }
                    #endregion

                    #region 条件为小于

                    if (logic == "小于")
                    {
                        switch (field)
                        {

                            case "单据日期":
                                predicate = predicate.And(p => p.odate < DateTime.Parse(value));
                                break;
                            case "总金额":
                                predicate = predicate.And(p => p.banlance < decimal.Parse(value));
                                break;
                            case "已结金额":
                                predicate = predicate.And(p => p.ysmoney < decimal.Parse(value));
                                break;
                        }
                    }
                    #endregion
                }
                return ViewProductSale_M(predicate,"", ACID);
            }
        }

        #endregion

        #region 销售退货单 接口实现增、改、删方法

        /// <summary>
        /// 销售单据退货新增保存
        /// </summary>
        /// <param name="m"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string ProductSaleBack_AddSave(ProductSaleBack_M m, int ACID)
        {
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        ProductSaleBack_M_Add(m, ACID, ctx);
                        foreach (ProductSaleBack_D d in m.ProductSaleBack_D)
                        {
                            ProductSaleBack_D_Add(d, ACID, ctx);
                        }
                        //退货增加库存
                        foreach (var kc in m.ProductSaleBack_D.GroupBy(p => p.goodscode))
                        {
                            onhand o = ctx.onhand.Single(p => p.GoodsCode == kc.Key && p.WarehouseID == m.warehouseID);
                            o.onhandcount += kc.Sum(p => p.count);
                        }
                        //应该记录应付账款,原来金额里减，所以这里用负数
                        RecordBalance(-m.banlance.Value, m.clientID.Value, ctx, ACID);
                        ctx.SubmitChanges();
                        tc.Complete();
                        r = "";
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }

        /// <summary>
        /// 产品销售退货--修改方法
        /// </summary>
        /// <param name="m">产品销售主表</param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string ProductSaleBack_UpdateSave(ProductSaleBack_M m, int ACID)
        {
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        //先获得修改前的对象
                        ProductSaleBack_M oldPmitem = ctx.ProductSaleBack_M.Single(p => p.djn == m.djn);
                        List<ProductSaleBack_D> oldpdlist = ctx.ProductSaleBack_D.Where(p => p.djn == m.djn).ToList();
                        ProductSaleBack_M_Update(m, ACID, ctx);
                        //再执行保存明细
                        foreach (ProductSaleBack_D d in m.ProductSaleBack_D)
                        {
                            ProductSaleBack_D nd = new ProductSaleBack_D();
                            nd.djn = d.djn;
                            nd.goodscode = d.goodscode;
                            nd.goodsname = d.goodsname;
                            nd.unit = d.unit;
                            nd.saleprice = d.saleprice;
                            nd.backprice = d.backprice;
                            nd.count = d.count;
                            nd.saleamount = d.saleamount;
                            nd.backamount = d.backamount;
                            nd.remark = d.remark;
                            ProductSaleBack_D_Add(nd, ACID, ctx);
                        }
                        //删除原来的
                        foreach (ProductSaleBack_D oldd in oldpdlist)
                        {
                            ProductSaleBack_DByzid_Delete(oldd.zid.ToString(), ACID, ctx);
                        }

                        //那么删除退货明细明细时，就应该在库存表中减少原来增加的
                        if (!oldPmitem.ischeck)
                        {
                            foreach (var kc in oldpdlist.GroupBy(p => p.goodscode))
                            {
                                onhand o = ctx.onhand.Single(p => p.GoodsCode == kc.Key && p.WarehouseID == oldPmitem.warehouseID);

                                o.onhandcount -= kc.Sum(p => p.count);
                            }
                        }

                        //增加新修改的库存
                        foreach (var kc in m.ProductSaleBack_D.GroupBy(p => p.goodscode))
                        {
                            onhand o = ctx.onhand.Single(p => p.GoodsCode == kc.Key && p.WarehouseID == m.warehouseID);

                            o.onhandcount += kc.Sum(p => p.count);
                        }
                        
                        RecordBalance(m.banlance.Value - oldPmitem.banlance.Value, m.clientID.Value, ctx, ACID);
                        ctx.SubmitChanges();
                        tc.Complete();
                        r = "";
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;



        }

        /// <summary>
        /// 删除销售退货单
        /// </summary>
        /// <param name="djn"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string ProductSaleBack_Delete(string djn, int ACID)
        {
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        //先获得删除前的对象
                        ProductSaleBack_M oldPmitem = ctx.ProductSaleBack_M.Single(p => p.djn == djn);
                        List<ProductSaleBack_D> oldpdlist = ctx.ProductSaleBack_D.Where(p => p.djn == djn).ToList();

                        //删除原来的明细
                        foreach (ProductSaleBack_D oldd in oldpdlist)
                        {
                            ProductSaleBack_DByzid_Delete(oldd.zid.ToString(), ACID, ctx);
                        }
                        ProductSaleBack_MBydjn_Delete(djn, ACID, ctx);

                        //那么删除明细时，就应该在库存表中减少原来退回增加的
                        foreach (var kc in oldpdlist.GroupBy(p => p.goodscode))
                        {
                            onhand o = ctx.onhand.Single(p => p.GoodsCode == kc.Key && p.WarehouseID == oldPmitem.warehouseID);

                            o.onhandcount -= kc.Sum(p => p.count);
                        }

                        RecordBalance(oldPmitem.banlance.Value, oldPmitem.clientID.Value, ctx, ACID);
                        ctx.SubmitChanges();
                        tc.Complete();
                        r = "";
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }

        /// <summary>
        /// 通过条件查询销售退货单据
        /// </summary>
        /// <param name="field"></param>
        /// <param name="logic"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public List<View_ProductSaleBack_M> GetProductSaleBack_M_ByCondition(string field, string logic, string value, int ACID)
        {

            using (CardsServer.DAL.DataClassesDataContext cxt = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                Expression<Func<View_ProductSaleBack_M, bool>> predicate = PredicateBuilder.True<View_ProductSaleBack_M>();
                if (!string.IsNullOrEmpty(field) && !string.IsNullOrEmpty(logic) && !string.IsNullOrEmpty(value))
                {
                    #region 条件为等于

                    if (logic == "等于")
                    {
                        switch (field)
                        {
                            case "单据号":
                                predicate = predicate.And(p => p.djn.Equals(value));
                                break;
                            case "单据日期":
                                predicate = predicate.And(p => p.odate >= DateTime.Parse(DateTime.Parse(value).ToString("yyyy-MM-dd") + " 00:00:00"));
                                predicate = predicate.And(p => p.odate <= DateTime.Parse(DateTime.Parse(value).ToString("yyyy-MM-dd") + " 23:59:59"));
                                break;
                            case "客户名称":
                                predicate = predicate.And(p => p.ClientName.Equals(value));
                                break;
                            case "仓库名称":
                                predicate = predicate.And(p => p.WarehouseName.Equals(value));
                                break;
                            case "总金额":
                                predicate = predicate.And(p => p.banlance.Equals(value));
                                break;
                            case "已结金额":
                                predicate = predicate.And(p => p.yfmoney.Equals(value));
                                break;
                            case "联系人":
                                predicate = predicate.And(p => p.linkman.Equals(value));
                                break;
                            case "联系电话":
                                predicate = predicate.And(p => p.linktel.Equals(value));
                                break;
                            case "联系地址":
                                predicate = predicate.And(p => p.linkaddress.Equals(value));
                                break;
                            case "业务员":
                                predicate = predicate.And(p => p.jsusername.Equals(value));
                                break;
                        }
                    }
                    #endregion

                    #region 条件为包含

                    if (logic == "包含")
                    {
                        switch (field)
                        {
                            case "单据号":
                                predicate = predicate.And(p => p.djn.Contains(value));
                                break;
                            case "单据日期":
                                predicate = predicate.And(p => p.odate.ToString().Contains(value));
                                break;
                            case "客户名称":
                                predicate = predicate.And(p => p.ClientName.Contains(value));
                                break;
                            case "仓库名称":
                                predicate = predicate.And(p => p.WarehouseName.Contains(value));
                                break;
                            case "总金额":
                                predicate = predicate.And(p => p.banlance.ToString().Contains(value));
                                break;
                            case "已结金额":
                                predicate = predicate.And(p => p.yfmoney.ToString().Contains(value));
                                break;
                            case "联系人":
                                predicate = predicate.And(p => p.linkman.Contains(value));
                                break;
                            case "联系电话":
                                predicate = predicate.And(p => p.linktel.Contains(value));
                                break;
                            case "联系地址":
                                predicate = predicate.And(p => p.linkaddress.Contains(value));
                                break;
                            case "业务员":
                                predicate = predicate.And(p => p.jsusername.Contains(value));
                                break;
                        }
                    }
                    #endregion

                    #region 条件为大于

                    if (logic == "大于")
                    {
                        switch (field)
                        {

                            case "单据日期":
                                predicate = predicate.And(p => p.odate > DateTime.Parse(value));
                                break;
                            case "总金额":
                                predicate = predicate.And(p => p.banlance > decimal.Parse(value));
                                break;
                            case "已结金额":
                                predicate = predicate.And(p => p.yfmoney > decimal.Parse(value));
                                break;
                        }
                    }
                    #endregion

                    #region 条件为小于

                    if (logic == "小于")
                    {
                        switch (field)
                        {

                            case "单据日期":
                                predicate = predicate.And(p => p.odate < DateTime.Parse(value));
                                break;
                            case "总金额":
                                predicate = predicate.And(p => p.banlance < decimal.Parse(value));
                                break;
                            case "已结金额":
                                predicate = predicate.And(p => p.yfmoney < decimal.Parse(value));
                                break;
                        }
                    }
                    #endregion
                }
                return cxt.View_ProductSaleBack_M.Where(predicate).ToList();
                //return ViewProductSaleBack_M(predicate, "", ACID);
            }
        }


        #endregion

        #region 销售报表接口实现方法
        
        /// <summary>
        /// 产品销售交易汇总表
        /// </summary>
        /// <param name="sd"></param>
        /// <param name="ed"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<Get_ProductSale_ByGoodsResult> Get_ProductSale_ByGoodsResult(DateTime? sd,DateTime? ed, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                if (sd == null || ed == null)
                {
                    sd = DateTime.Parse("1900-01-01 00:00:00");
                    ed = DateTime.Parse("2999-12-30 23:59:59");
                }
                return ctx.Get_ProductSale_ByGoods(sd, ed).ToList();
            }
        }

        /// <summary>
        /// 客户销售交易汇总表
        /// </summary>
        /// <param name="sd"></param>
        /// <param name="ed"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<Get_ProductSale_ByClientResult> Get_ProductSale_ByClientResult(DateTime? sd, DateTime? ed, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                if (sd == null || ed == null)
                {
                    sd = DateTime.Parse("1900-01-01 00:00:00");
                    ed = DateTime.Parse("2999-12-30 23:59:59");
                }
                return ctx.Get_ProductSale_ByClient(sd, ed).ToList();
            }
        }

        /// <summary>
        /// 业务员销售交易汇总表
        /// </summary>
        /// <param name="sd"></param>
        /// <param name="ed"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<Get_ProductSale_ByEmpResult> Get_ProductSale_ByEmpResult(DateTime? sd, DateTime? ed, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                if (sd == null || ed == null)
                {
                    sd = DateTime.Parse("1900-01-01 00:00:00");
                    ed = DateTime.Parse("2099-12-30 23:59:59");
                }
                return ctx.Get_ProductSale_ByEmp(sd, ed).ToList();
            }
        }

        #endregion


        public List<View_Onhand> Get_Onhand(int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.View_Onhand.ToList();
            }
        }
    }

}
